import { defineStore } from "pinia";
import { Local } from "@/utils/storage";
import { getCommodityDetail } from "@/api/product";
import { createDiscreteApi } from "naive-ui";
const { message } = createDiscreteApi(["message"], {});
export const useCompare = defineStore("compare", {
  state: () => ({
    compareGoods: [],
  }),
  actions: {
    loadCompareGoods() {
      let goodsList = Local.get("compareGoods") || [];
      if (goodsList.length > 0) {
        this.compareGoods = goodsList;
      }
    },
    remove(offerId) {
      this.compareGoods = this.compareGoods.filter((x) => x.offerId != offerId);
      Local.set("compareGoods", this.compareGoods);
    },
    async addCompareGoods(offerId) {
      let goodsList = Local.get("compareGoods") || [];
      if (goodsList.filter((x) => x.offerId == offerId).length > 0) {
        // //该商品已追加，请选择其他商品。
        // message.warning(
        //   "Este producto ha sido agregado, por favor seleccione otro producto."
        // );
        this.remove(offerId);
        return;
      }
      const { data } = await getCommodityDetail(offerId);
      const goods = data.data;
      goodsList.push(goods);
      this.compareGoods = goodsList;
      Local.set("compareGoods", goodsList);
      message.success("Operación exitosa");
    },
    isAdd(offerId) {
      let goodsList = Local.get("compareGoods") || [];
      return goodsList.find((x) => x.offerId == offerId) ? true : false;
    },
  },
});
